﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using ipaidabribe.Code;

namespace ipaidabribe
{
    /// <summary>
    /// Summary description for LoadAllArticle
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class LoadAllArticle : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        [WebMethod]
        public List<BribeService> SelectAll()
        {
            var db = new ipaidabribeEntities();
          
            var lastest = from bribe in db.Bribes.OrderByDescending(t => t.Id)
                          select new BribeService
                          {
                              Id = bribe.Id,
                              Cat = bribe.Cat,
                              City = bribe.City,
                              Departments = bribe.Departments,
                              Transaction = bribe.Transaction,
                              AmountPaid = bribe.AmountPaid,
                              DatePaid = bribe.DatePaid,
                              OfficeLocation = bribe.OfficeLocation,
                              ResistedABribe = bribe.ResistedABribe,
                              BribeType = bribe.BribeType,
                              Title = bribe.Title,
                              Content = bribe.Content,
                              DateReport = bribe.DateReport,
                              TotalComment = bribe.TotalComment,
                              Enable = bribe.Enable,
                              Link = Utils.ToFriendlyUrl(HttpUtility.HtmlDecode(bribe.Title), bribe.Id.ToString())
                          };
            return lastest.ToList();
        }

        [WebMethod]
        public List<BribeServiceSimple> SelectSimple(int topX)
        {
            var db = new ipaidabribeEntities();

            var lastest = db.Bribes.Where(t => t.Enable != false).OrderByDescending(t => t.Id).Take(topX).ToList();

            List<BribeServiceSimple> returnList = new List<BribeServiceSimple>();

            foreach (var bribe in lastest)
            {
                BribeServiceSimple item = new BribeServiceSimple();
                          
                item.AmountPaid = bribe.AmountPaid;
                item.Title = bribe.Title;
                item.Content = bribe.Content;
                item.Link = "http://toidihoilo.com/" + Utils.ToFriendlyUrl(HttpUtility.HtmlDecode(bribe.Title), bribe.Id.ToString());
                
                returnList.Add(item);
            }
            return returnList;
        }
        public class BribeService
        {
          
            public int Id { get; set; }
            public int? Cat { get; set; }
            public string City { get; set; }
            public string Departments { get; set; }
            public string Transaction { get; set; }
            public string AmountPaid { get; set; }
            public DateTime? DatePaid { get; set; }
            public string OfficeLocation { get; set; }
            public string ResistedABribe { get; set; }
            public string BribeType { get; set; }
            public string Title { get; set; }
            public string Content { get; set; }
            public DateTime? DateReport { get; set; }
            public int? TotalComment { get; set; }
            public bool? Enable { get; set; }
            public string Link { get; set; }

        }
        public class BribeServiceSimple
        {
            public string AmountPaid { get; set; }
            public string Title { get; set; }
            public string Content { get; set; }
            public string Link { get; set; }
        }
        
    }
}
